{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2fe16c2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "max_cap_len = 15  # Determines max length of captioning sentences\n",
    "img_dimension = 299 # Determines the height and width of images\n",
    "num_words = 10000 # Determines vocab size to tokenize and train on\n",
    "encoding_size = 512 # Determines dimension of the encodings of images\n",
    "LSTM_size = 512\n",
    "batch_size = 128\n",
    "n_epochs = 15\n",
    "Buffer_size = 1000\n",
    "validation_and_test_split = 0.2\n",
    "test_to_val_split = 0.5\n",
    "num_examples = None # Determines number of overall read samples. If set to none all samples will be read as long as they don't exceed max_cap_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d496e5ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "PATH = r'D:\\datasets\\COCO\\train\\train2014'\n",
    "# Read the json file\n",
    "annotation_file = r'D:\\datasets\\COCO\\annotations_trainval2014\\annotations\\captions_train2014.json'\n",
    "with open(annotation_file, 'r') as f:\n",
    "    annotations = json.load(f)\n",
    "\n",
    "# Store captions and image names in vectors\n",
    "all_captions = []\n",
    "# all_img_name_vector = []\n",
    "cap_dic={}\n",
    "for annot in annotations['annotations']:\n",
    "    caption = annot['caption']\n",
    "    image_id = annot['image_id']\n",
    "    full_coco_image_path = 'COCO_train2014_' + '%012d.jpg' % (image_id)\n",
    "    # all_img_name_vector.append(full_coco_image_path)\n",
    "    if full_coco_image_path in cap_dic:\n",
    "        cap_dic[full_coco_image_path].append(caption)\n",
    "    else:\n",
    "        cap_dic[full_coco_image_path] = [caption]\n",
    "#     all_captions.append((full_coco_image_path, caption))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1d57a98a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7 5\n"
     ]
    }
   ],
   "source": [
    "maxitem=0\n",
    "minitem = 7\n",
    "for a in cap_dic:\n",
    "    if len(cap_dic[a]) > maxitem:\n",
    "        maxitem = len(cap_dic[a])\n",
    "    if len(cap_dic[a])< minitem:\n",
    "        minitem = len(cap_dic[a])\n",
    "print(maxitem,minitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "20cd2f81",
   "metadata": {},
   "outputs": [],
   "source": [
    "# with open('COCO_caption.txt', \"w\", encoding=\"utf8\") as f:\n",
    "#     # line = 'image_path, caption'+'\\n'\n",
    "#     # f.write(line)\n",
    "#     for imgpath,caption in all_captions:\n",
    "#         caption = caption.replace('\\n', '').replace('\\r', '')\n",
    "#         line = imgpath+'#0\\t'+caption\n",
    "#         if line[-1] != '\\n':\n",
    "#             line += '\\n'\n",
    "#         f.write(line)\n",
    "\n",
    "with open('COCO_caption.txt', \"w\", encoding=\"utf8\") as f:\n",
    "    # line = 'image_path, caption'+'\\n'\n",
    "    # f.write(line)\n",
    "    for imgpath in cap_dic:\n",
    "        captions = cap_dic[imgpath]\n",
    "        i = 0\n",
    "        for caption in captions:\n",
    "            caption = caption.replace('\\n', '').replace('\\r', '')\n",
    "            line = imgpath+'#'+str(i)+'\\t'+caption\n",
    "            if line[-1] != '\\n':\n",
    "                line += '\\n'\n",
    "            f.write(line)\n",
    "            i+=1\n",
    "            if i >= 5 : break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e1e0a14",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
